package com.msu.nava.activity;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.msu.nava.R;
import com.msu.nava.modle.CI;

import java.util.List;
import java.util.Map;
import java.util.Set;

import de.halfbit.pinnedsection.PinnedSectionListView;

public class CIListActivity extends ListActivity implements OnClickListener {
  private static CI ci = new CI();

  @Override
  public void onClick(View v) {
    Log.d("ciclick", "onClick:item ");
    Toast.makeText(this, "Item: " + v.getTag(), Toast.LENGTH_SHORT).show();

  }//设置边距
  private void initializePadding() {
    float density = getResources().getDisplayMetrics().density;
    int padding =  (int) (16 * density) ;
    getListView().setPadding(padding, padding, padding, padding);
  }
  //在这个函数中设置每一条的监听函数而不是onClick()函数
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Item item = (Item) getListView().getAdapter().getItem(position);
    if (item != null) {

      Intent intent = new Intent(CIListActivity.this,DiagnoseActivity.class);
      intent.putExtra("iname",item.text);//将用户的选择放入intent
      startActivity(intent);

    } else {
      Toast.makeText(this, "获取item失败~" + position, Toast.LENGTH_SHORT).show();
    }
  }
  static class Item {

    public static final int ITEM = 0;         //表示改该项是选项
    public static final int SECTION = 1;      //表示该项是标题

    public final int type;
    public final String text;

    public int sectionPosition;
    public int listPosition;

    public Item(int type, String text) {
      this.type = type;
      this.text = text;
    }

    @Override
    public String toString() {
      return text;
    }

  }
  static class SimpleAdapter extends ArrayAdapter<Item> implements PinnedSectionListView.PinnedSectionListAdapter {

    private static final int[] COLORS = new int[]{
      R.color.erke, R.color.wgk,
      R.color.wk, R.color.nk,R.color.fk};

    public SimpleAdapter(Context context, int resource, int textViewResourceId) {
      super(context, resource, textViewResourceId);
      generateDataset();
    }

    public void generateDataset( ) {

      Map<String, List<String>> cimap = ci.getCimap();
      Set<String> categorys = cimap.keySet();
      final int sectionsNumber = categorys.size();//组的数量
      int sectionPosition = 0, listPosition = 0;
      for(String category:categorys){
        Item ct = new Item(Item.SECTION,category);
        ct.sectionPosition = sectionPosition;
        ct.listPosition = listPosition++;
        add(ct);
        List<String> ills = cimap.get(category);
        for(String ill:ills){
          Item n1 = new Item(Item.ITEM, ill);
          n1.sectionPosition = sectionPosition;
          n1.listPosition = listPosition++;
          add(n1);
        }
        sectionPosition++;
      }


    }

    protected void prepareSections(int sectionsNumber) {
    }

    protected void onSectionAdded(Item section, int sectionPosition) {
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      TextView view = (TextView) super.getView(position, convertView, parent);
      view.setTextColor(Color.DKGRAY);
      view.setTag("" + position);
      Item item = getItem(position);
      if (item.type == Item.SECTION) {
        view.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
      }
      return view;
    }

    @Override
    public int getViewTypeCount() {
      return 2;
    }

    @Override
    public int getItemViewType(int position) {
      return getItem(position).type;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
      return viewType == Item.SECTION;
    }

  }


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ci);
    initializePadding();//设置边距
    initListView();
    gotoSelectedCategory();
  }
  //计算位置并跳转到相应的科室下
  private void gotoSelectedCategory(){
    String category = getIntent().getStringExtra("category");
    int pos = 0;
    CI ci = new CI();
    Set<String> cas= ci.getCimap().keySet();
    for(String ca : cas){
      if(category.equals(ca)){
        break;
      }else {
        pos +=ci.getCimap().get(ca).size();
      }
      pos++;
    }
    setSelection(pos);
  }

  private void initListView(){
    setListAdapter(new SimpleAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1));
  }
}
